/*

Copyright 2009 Michel Boto. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "../PreOrder.h"
#include "File.h"
#include <iostream>
#include <list>
#include <string>
#include <tr1/memory>
#include <gtest/gtest.h>

/*
	A simple example app showing a useful application of the preorder iterator for depth-first searching
 */

using namespace TestDataStructures;

struct PreOrderFileIterator : public Blatt::PreOrder<PreOrderFileIterator,FileRef,FileDependencyIterator>
{
        typedef Blatt::PreOrder<PreOrderFileIterator,FileRef,FileDependencyIterator> TBase;
        
        PreOrderFileIterator()
        : TBase() 
        {}
        
        explicit PreOrderFileIterator( FileRef& node )
        : TBase( node ) 
        {}
        
        TChildItHolder GetChildRangeForNode( const FileRef& fileRef ) const
		{ 
			const FilePtr filePtr( fileRef.lock() );
			return TChildItHolder( filePtr->dependencies().begin(), filePtr->dependencies().end() ); 
		}
		
		friend std::ostream& operator<<(std::ostream&,const PreOrderFileIterator&);
};

std::ostream &operator<<(std::ostream& stream, const PreOrderFileIterator& it)
{
	if ( it.GetRootNode() )
	{
		const FilePtr file( it.GetRootNode()->lock() );
		return stream << *file.get();
	}
	return stream << "[Null]";
}

TEST(PreOrderFileDependTest,EndIsEnd)
{
	ASSERT_EQ( PreOrderFileIterator(), PreOrderFileIterator() );
}

TEST(PreOrderFileDependTest,CanConstructIterator)
{
	FilePtr a( new File("a" ) );
	FileRef refToA( a );
	ASSERT_NO_THROW( PreOrderFileIterator( refToA ) );
}

TEST(PreOrderFileDependTest,EndIsNotNotEnd)
{
	FilePtr a( new File("a" ) );
	FileRef refToA( a );
	ASSERT_NE( PreOrderFileIterator(refToA), PreOrderFileIterator() );
}

TEST(PreOrderFileDependTest,NotEndIsNotEnd)
{
	FilePtr a( new File("a" ) );
	FileRef refToA( a );
	FileRef anotherRefToA( a );
	ASSERT_EQ( PreOrderFileIterator(refToA), PreOrderFileIterator(anotherRefToA) );
}

TEST(PreOrderFileDependTest,SizeIsOne)
{
	FilePtr a( new File("a" ) );
	FileRef refToA( a );
	PreOrderFileIterator it( refToA );
	PreOrderFileIterator itEnd;
	ASSERT_NE( it, itEnd );
	++it;
	ASSERT_EQ( it, itEnd );
}

TEST(PreOrderFileDependTest,FirstItIsA)
{
	FilePtr a( new File("a" ) );
	FileRef refToA( a );
	PreOrderFileIterator it( refToA );
	ASSERT_TRUE( !it->expired() );
	ASSERT_EQ( "a", it->lock()->name() );
}

TEST(PreOrderFileDependTest,SecondItIsB)
{
	FilePtr a( new File("a" ) );
	FilePtr b( new File("b") );
	a->addDependency( b );
	FileRef refToA( a );
	PreOrderFileIterator it( refToA );
	++it;
	ASSERT_TRUE( !it->expired() );
	ASSERT_EQ( "b", it->lock()->name() );
}

TEST(PreOrderFileDependTest,ThirdIsC)
{
	FilePtr a( new File("a" ) );
	FilePtr b( new File("b") );
	a->addDependency( b );
	FilePtr c( new File("c") );
	a->addDependency( c );
	FileRef refToA( a );
	PreOrderFileIterator it( refToA );
	++it;++it;
	ASSERT_TRUE( !it->expired() );
	ASSERT_EQ( "c", it->lock()->name() );
}

template <class OutIt>
void topologicalSort( PreOrderFileIterator& it, PreOrderFileIterator end, OutIt out, std::set<File*>& alreadyVisited ) {

	while ( it != end ) {
		FileRef fileRef( *it );
		FilePtr filePtr( fileRef.lock() );
		if ( alreadyVisited.find( filePtr.get() ) == alreadyVisited.end() ) {
			alreadyVisited.insert( filePtr.get() );
			
			if ( !filePtr->dependencies().empty() )
			{
				PreOrderFileIterator itChild( fileRef );
				++itChild; // we're already processing fileRef, so skip to the next node in the list
				topologicalSort( itChild, end, out, alreadyVisited );
			}
			
			*out++ = filePtr;
		}
		++it;
	}
}

TEST(PreOrderFileDependTest,SortOneFile)
{
	FilePtr a( new File("a") );
	std::vector<FilePtr> sortedFiles;
	FileRef aRef( a );
	std::set<File*> alreadyVisited;
	PreOrderFileIterator root( aRef );
	topologicalSort( root, PreOrderFileIterator(), std::back_inserter( sortedFiles ), alreadyVisited );
	ASSERT_EQ( 1, sortedFiles.size() );
	ASSERT_EQ( a, *sortedFiles.begin() );
}

TEST(PreOrderFileDependTest,SortTwoIndependentFilesAndAProject)
{
	FilePtr project( new File("project") );
	FilePtr file1( new File("file1") );
	project->addDependency( file1 );
	FilePtr file2( new File("file2") );
	project->addDependency( file2 );
	std::vector<FilePtr> sortedFiles;
	FileRef projectRef( project );
	std::set<File*> alreadyVisited;
	PreOrderFileIterator root( projectRef );
	topologicalSort( root, PreOrderFileIterator(), std::back_inserter( sortedFiles ), alreadyVisited );
	std::vector<FilePtr>::const_iterator itFile( sortedFiles.begin() );
	ASSERT_EQ( 3, sortedFiles.size() );
	ASSERT_EQ( file1, *itFile );
	++itFile;
	ASSERT_EQ( file2, *itFile );
	++itFile;
	ASSERT_EQ( project, *itFile );
}

TEST(PreOrderFileDependTest,SortOneDependentFileOneIndependentAndAProject)
{
	FilePtr project( new File("project") );
	FilePtr file1( new File("file1") );
	project->addDependency( file1 );
	FilePtr file2( new File("file2") );
	project->addDependency( file2 );
	file2->addDependency( file1 ); // now file2 depends on file1
	std::vector<FilePtr> sortedFiles;
	FileRef projectRef( project );
	std::set<File*> alreadyVisited;
	PreOrderFileIterator root( projectRef );
	topologicalSort( root, PreOrderFileIterator(), std::back_inserter( sortedFiles ), alreadyVisited );
	std::vector<FilePtr>::const_iterator itFile( sortedFiles.begin() );
	ASSERT_EQ( 3, sortedFiles.size() );
	ASSERT_EQ( file1, *itFile );
	++itFile;
	ASSERT_EQ( file2, *itFile );
	++itFile;
	ASSERT_EQ( project, *itFile );
}

TEST(PreOrderFileDependTest,SortTwoDependsOneIndependentAndAProject)
{
	FilePtr project( new File("project") );
	FilePtr file1( new File("file1") );
	project->addDependency( file1 );
	FilePtr file2( new File("file2") );
	project->addDependency( file2 );
	file2->addDependency( file1 ); // now file2 depends on file1
	FilePtr file3( new File("file3") );
	project->addDependency( file3 );
	file3->addDependency( file2 );
	file3->addDependency( file1 );
	std::vector<FilePtr> sortedFiles;
	FileRef projectRef( project );
	std::set<File*> alreadyVisited;
	PreOrderFileIterator root( projectRef ); 
	topologicalSort( root, PreOrderFileIterator(), std::back_inserter( sortedFiles ), alreadyVisited );
	std::vector<FilePtr>::const_iterator itFile( sortedFiles.begin() );
	ASSERT_EQ( 4, sortedFiles.size() );
	ASSERT_EQ( file1, *itFile );
	++itFile;
	ASSERT_EQ( file2, *itFile );
	++itFile;
	ASSERT_EQ( file3, *itFile );
	++itFile;
	ASSERT_EQ( project, *itFile );
}

TEST(PreOrderFileDependTest,SortThisNightmare)
{
	FilePtr project( new File("project") );
	FilePtr file1( new File("file1") );
	project->addDependency( file1 );
	FilePtr file2( new File("file2") );
	project->addDependency( file2 );
	FilePtr file3( new File("file3") );
	project->addDependency( file3 );
	FilePtr file4( new File("file4") );
	project->addDependency( file4 );
	FilePtr file5( new File("file5") );
	project->addDependency( file5 );
	FilePtr file6( new File("file6") );
	project->addDependency( file6 );
	FilePtr file7( new File("file7") );
	project->addDependency( file7 );
	FilePtr file8( new File("file8") );
	project->addDependency( file8 );
	FilePtr file9( new File("file9") );
	project->addDependency( file9 );
	file1->addDependency( file3 );
	file1->addDependency( file4 );
	file1->addDependency( file5 );
	file2->addDependency( file1 );
	file2->addDependency( file6 );
	file2->addDependency( file7 );
	file6->addDependency( file1 );
	file3->addDependency( file8 );
	file3->addDependency( file9 );
	
	std::vector<FilePtr> sortedFiles;
	FileRef projectRef( project );
	std::set<File*> alreadyVisited;
	PreOrderFileIterator root( projectRef ); 
	
	topologicalSort( root, PreOrderFileIterator(), std::back_inserter( sortedFiles ), alreadyVisited );
	std::vector<FilePtr>::const_iterator itFile( sortedFiles.begin() );
	ASSERT_EQ( 10, sortedFiles.size() );
	ASSERT_EQ( *itFile++, file8 );
	ASSERT_EQ( *itFile++, file9 );
	ASSERT_EQ( *itFile++, file3 );
	ASSERT_EQ( *itFile++, file4 );
	ASSERT_EQ( *itFile++, file5 );
	ASSERT_EQ( *itFile++, file1 );
	ASSERT_EQ( *itFile++, file6 );
	ASSERT_EQ( *itFile++, file7 );
	ASSERT_EQ( *itFile++, file2 );
	ASSERT_EQ( *itFile++, project );
}
